﻿using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GSF
{
    public struct TextureInfo
    {
        public string path;
        public int cols;
        public int rows;
    }
    public class ResourceMgr
    {
        static ResourceMgr m_instance;
        public static ResourceMgr Instance
        {
            get
            {
                if(m_instance == null)
                    m_instance = new ResourceMgr();
                return m_instance;
            }
        }

        ResourceMgr()
        {
        }

        List<TextureInfo> m_enemyList;
        List<TextureInfo> m_skillList;
        List<TextureInfo> m_towerList;
        List<TextureInfo> m_effectList;

        List<string> m_terrainList;
        public void Init()
        {
            m_enemyList = new List<TextureInfo>();
            m_skillList = new List<TextureInfo>();
            m_towerList = new List<TextureInfo>();
            m_effectList = new List<TextureInfo>();
            m_terrainList = new List<string>();
            string path = @"ResourcesManager.txt";
            if (!File.Exists(path))
            {
                File.Create(path);
                return;
            }
            using (StreamReader sr = File.OpenText(path))
            {
                //load terrains
                string str = sr.ReadLine();
                string[] words = str.Split(' ');
                int terrainsCount = int.Parse(words[1]);
                for (int i = 0; i < terrainsCount; i++)
                {
                    sr.ReadLine();
                    m_terrainList.Add(sr.ReadLine());
                }

                //load enemies
                str = sr.ReadLine();
                words = str.Split(' ');
                int enemiesCount = int.Parse(words[1]);
                for (int i = 0; i < enemiesCount; i++)
                {
                    sr.ReadLine();
                    TextureInfo newInfo = new TextureInfo();
                    str = sr.ReadLine();
                    words = str.Split(' ');
                    newInfo.path = words[0];
                    newInfo.rows = int.Parse(words[1]);
                    newInfo.cols = int.Parse(words[2]);
                    m_enemyList.Add(newInfo);
                }

                //load skills
                str = sr.ReadLine();
                words = str.Split(' ');
                int skillsCount = int.Parse(words[1]);
                for (int i = 0; i < enemiesCount; i++)
                {
                    sr.ReadLine();
                    TextureInfo newInfo = new TextureInfo();
                    str = sr.ReadLine();
                    words = str.Split(' ');
                    newInfo.path = words[0];
                    newInfo.rows = int.Parse(words[1]);
                    newInfo.cols = int.Parse(words[2]);
                    m_skillList.Add(newInfo);
                }
                //load towers
                str = sr.ReadLine();
                words = str.Split(' ');
                int towersCount = int.Parse(words[1]);
                for (int i = 0; i < towersCount; i++)
                {
                    sr.ReadLine();
                    TextureInfo newInfo = new TextureInfo();
                    str = sr.ReadLine();
                    words = str.Split(' ');
                    newInfo.path = words[0];
                    newInfo.rows = int.Parse(words[1]);
                    newInfo.cols = int.Parse(words[2]);
                    m_towerList.Add(newInfo);
                }
                //load effect
                str = sr.ReadLine();
                words = str.Split(' ');
                int effectsCount = int.Parse(words[1]);
                for (int i = 0; i < effectsCount; i++)
                {
                    sr.ReadLine();
                    TextureInfo newInfo = new TextureInfo();
                    str = sr.ReadLine();
                    words = str.Split(' ');
                    newInfo.path = words[0];
                    newInfo.rows = int.Parse(words[1]);
                    newInfo.cols = int.Parse(words[2]);
                    m_effectList.Add(newInfo);
                }
            }
        }

        public string getTerrainPath(int id)
        {
            return m_terrainList[id];
        }

        public TextureInfo getEnemy(int id)
        {
            return m_enemyList[id];
        }

        public TextureInfo getEffect(int id)
        {
            return m_effectList[id];
        }
    }
}
